"""
created on 2023-03-12
author: zjh
description： collect Resourcemanager jmx metrics
"""
import argparse
import logging
import parser
import random
import threading
import time

import requests
import prometheus_client
from prometheus_client import Counter, Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
from prometheus_client.metrics import Summary
from psutil import virtual_memory
from psutil import cpu_times
import json


#lspath=['metrics','scheduler']

#获取active状态的Resourcemanager节点
class Baseinfo:
    def getactivenode(self,list_ip):
        for ip in list_ip:
            rmurl = str('http://') + ip + str('/ws/v1/cluster')
            try:
                html = requests.get(rmurl)
                hjson = json.loads(html.text)
                if hjson['clusterInfo']['haState'] == 'ACTIVE':
                    #print(hjson["clusterInfo"])
                    #print(ip + ' is active')
                    return ip
            except:

                print(ip + " is standby")


app = Flask(__name__)



REGISTRY = CollectorRegistry(auto_describe=False)
count = Counter(
    'count',
    'count',
    registry=REGISTRY
)
appsSubmitted = Gauge(
 'appsSubmitted',
 'appsSubmitted',
 registry=REGISTRY
)
appsCompleted = Gauge(
 'appsCompleted',
 'appsCompleted',
 registry=REGISTRY
)
appsPending = Gauge(
 'appsPending',
 'appsPending',
 registry=REGISTRY
)
appsRunning = Gauge(
 'appsRunning',
 'appsRunning',
 registry=REGISTRY
)
appsFailed = Gauge(
 'appsFailed',
 'appsFailed',
 registry=REGISTRY
)
appsKilled = Gauge(
 'appsKilled',
 'appsKilled',
 registry=REGISTRY
)
reservedMB = Gauge(
 'reservedMB',
 'reservedMB',
 registry=REGISTRY
)
availableMB = Gauge(
 'availableMB',
 'availableMB',
 registry=REGISTRY
)
allocatedMB = Gauge(
 'allocatedMB',
 'allocatedMB',
 registry=REGISTRY
)
reservedVirtualCores = Gauge(
 'reservedVirtualCores',
 'reservedVirtualCores',
 registry=REGISTRY
)
availableVirtualCores = Gauge(
 'availableVirtualCores',
 'availableVirtualCores',
 registry=REGISTRY
)
allocatedVirtualCores = Gauge(
 'allocatedVirtualCores',
 'allocatedVirtualCores',
 registry=REGISTRY
)
containersAllocated = Gauge(
 'containersAllocated',
 'containersAllocated',
 registry=REGISTRY
)
containersReserved = Gauge(
 'containersReserved',
 'containersReserved',
 registry=REGISTRY
)
totalMB = Gauge(
 'totalMB',
 'totalMB',
 registry=REGISTRY
)
totalVirtualCores = Gauge(
 'totalVirtualCores',
 'totalVirtualCores',
 registry=REGISTRY
)
lostNodes = Gauge(
 'lostNodes',
 'lostNodes',
 registry=REGISTRY
)
unhealthyNodes = Gauge(
 'unhealthyNodes',
 'unhealthyNodes',
 registry=REGISTRY
)
decommissionedNodes = Gauge(
 'decommissionedNodes',
 'decommissionedNodes',
 registry=REGISTRY
)
rebootedNodes = Gauge(
 'rebootedNodes',
 'rebootedNodes',
 registry=REGISTRY
)
activeNodes = Gauge(
 'activeNodes',
 'activeNodes',
 registry=REGISTRY
)

c = Gauge('my_requests_total', 'HTTP Failures', ['method', 'endpoint'],registry=REGISTRY)


@app.route('/metrics')
def r_value():
    #lspath = ['metrics','scheduler']
    #for path in lspath:
    appsSubmitted.set(hjson['clusterMetrics']['appsSubmitted'])
    appsCompleted.set(hjson['clusterMetrics']['appsCompleted'])
    appsPending.set(hjson['clusterMetrics']['appsPending'])
    appsRunning.set(hjson['clusterMetrics']['appsRunning'])
    appsFailed.set(hjson['clusterMetrics']['appsFailed'])
    appsKilled.set(hjson['clusterMetrics']['appsKilled'])
    reservedMB.set(hjson['clusterMetrics']['reservedMB'])
    availableMB.set(hjson['clusterMetrics']['availableMB'])
    allocatedMB.set(hjson['clusterMetrics']['allocatedMB'])
    reservedVirtualCores.set(hjson['clusterMetrics']['reservedVirtualCores'])
    availableVirtualCores.set(hjson['clusterMetrics']['availableVirtualCores'])
    allocatedVirtualCores.set(hjson['clusterMetrics']['allocatedVirtualCores'])
    containersAllocated.set(hjson['clusterMetrics']['containersAllocated'])
    containersReserved.set(hjson['clusterMetrics']['containersPending'])
    totalMB.set(hjson['clusterMetrics']['totalMB'])
    totalVirtualCores.set(hjson['clusterMetrics']['totalVirtualCores'])
    lostNodes.set(hjson['clusterMetrics']['lostNodes'])
    unhealthyNodes.set(hjson['clusterMetrics']['unhealthyNodes'])
    decommissionedNodes.set(hjson['clusterMetrics']['decommissionedNodes'])
    rebootedNodes.set(hjson['clusterMetrics']['rebootedNodes'])
    activeNodes.set(hjson['clusterMetrics']['activeNodes'])


    c.labels('test', '1').inc()
    c.labels('post', '/submit').inc()
    return Response(prometheus_client.generate_latest(REGISTRY),
                    mimetype="text/plain")


@app.route('/')
def index():
    return "<html>" \
           "<head><title>Resourcemanager Exporter</title></head>" \
           "<body>" \
           "<h1>Resourcemanager Exporter</h1>" \
           "<p><a href=" + ('/metrics')+ ">Metrics</a></p></body>" \
                                            "</html>"
    #return "Resourcemanager metric address: ip:port/metrics"




if __name__ == "__main__":
    base_info=Baseinfo()
    #yarn集群主备节点
    resourcemanager_ip = ["192.168.10.10:8088","192.168.10.11:8088"]
    #对active状态的节点抓取metrics数据
    activeurl=base_info.getactivenode(resourcemanager_ip)
    # html = urllib.request.urlopen(rmurl)
    # hjson = json.loads(html.read())
    html = requests.get('http://' + activeurl + '/ws/v1/cluster/metrics')
    hjson = json.loads(html.text)
    app.run(host='localhost',port=9098,debug=True)
